package com.alstudio.utils;

import java.util.ArrayList;

import android.text.TextUtils;

import com.alstudio.utils.HanziToPinyin.Token;
import com.alstudio.utils.log.Mylog;

public class PinYin {
	//汉字返回拼音，字母原样返回，都转换为小写
	public static String getPinYin(String input) {
		ArrayList<Token> tokens = HanziToPinyin.getInstance().get(input);
		StringBuilder sb = new StringBuilder();
		if (tokens != null && tokens.size() > 0) {
			for (Token token : tokens) {
				if (Token.PINYIN == token.type) {
					sb.append(token.target);
				} else {
					sb.append(token.source);
				}
			}
		}
		return sb.toString().toUpperCase();
	}
	
	//汉字返回拼音，字母原样返回，都转换为小写
	public static String getFirstPinYinSpell(String chinese) {
		
		
		if (TextUtils.isEmpty(chinese)) {
			return "";
		}
		String firstSpell = chinese.trim();
//		firstSpell = firstSpell.;
		if(firstSpell.length() > 1){
			firstSpell = firstSpell.substring(0,1);
		}
		Mylog.e("第一个是 "+firstSpell);
		firstSpell = PinYin.getPinYin(firstSpell);
		char[] param = firstSpell.toCharArray();
		StringBuilder pinyin = new StringBuilder();
		
		try {
			pinyin.append(Character.toString(param[0]));

		} catch (Exception e) {
			e.printStackTrace();
		}
		String result = pinyin.toString().toUpperCase();
		if (!result.matches("[0-9a-zA-Z]")) {
			result = "#";
		}
		
		return result;
	}
}
